package com.eqdd.yiqidian.ui.mine.renwu;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.annotation.datasave.ToSave;
import com.eqdd.yiqidian.CreateRenwuActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.RenwuSearchBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditTimeBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.SelectMultiStaffActivity;
import com.eqdd.yiqidian.ui.select.SelectSingleStaffActivity;
import com.eqdd.yiqidian.utils.SPUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.io.Serializable;

public class CreateRenwuActivity extends SlimBaseMultiEditActivity {
    private CreateRenwuActivityCustom dataBinding;
    private String content;
    @ToSave
    String zerenren;
    private Bundle bundle;
    @ToSave

    String xiezuren;
    @ToSave
    String zhihuiren;
    @ToSave
    String yanshouren;
    @ToSave
    RenwuSearchBean renwuSearchBean;
    private String zerenrenName;
    private String renwuDesc;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_create_renwu);
        initTopTitleBar(View.VISIBLE, "创建任务");
        initTopRightText("创建", v -> {
            if (checkParam()) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.CREATE_RENWU)
                        .params("userGuid", user.getGuid())
                        .params("TaskName", datas.get(0).getContent())
                        .params("taskDesc", datas.get(1).getContent())
                        .params("ParentTaskId", renwuSearchBean == null ? 0 : renwuSearchBean.getId())
                        .params("recipient", zerenren)
                        .params("assist", xiezuren == null ? "" : xiezuren)
                        .params("notify", zhihuiren == null ? "" : zhihuiren)
                        .params("startTime", datas.get(6).getContent())
                        .params("endTime", datas.get(7).getContent())
                        .params("checkStandard", datas.get(8).getContent())
                        .params("checker", yanshouren)
                        .params("checkTime", datas.get(10).getContent())
                        .params("duty", datas.get(11).getContent())
                        .params("companyId", user.getCompanyId())
                        .params("departId", user.getDepartId())
                        .execute(new DialogCallBack<HttpResult>(this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    isSave = false;
                                    clearData();
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort("信息不完整，我没法向上面交代呀");
            }
        });
    }

    private String getAssist() {
        return null;
    }

    private boolean checkParam() {
        for (int i = 0; i < datas.size(); i++) {
            if (i == 2 || i == 4 || i == 5)
                continue;
            if (TextUtils.isEmpty(datas.get(i).getContent())) {
                return false;
            }
        }
        return true;
    }

    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (datas.get(p).getPosition()) {
            case 2:
                startActivityForResult(RenwuListSearchActivity.class, null, Config.RENWU_LIST_SEARCH);
                break;
            case 3:
            case 9:
                bundle = new Bundle();
                bundle.putString("departId", user.getDepartId() + "");
                bundle.putString("companyId", user.getCompanyId() + "");
                startActivityForResult(SelectSingleStaffActivity.class, bundle, p);
                break;
            case 4:
            case 5:
                bundle = new Bundle();
                bundle.putString("departId", user.getDepartId() + "");
                bundle.putString("companyId", user.getCompanyId() + "");
                startActivityForResult(SelectMultiStaffActivity.class, bundle, p);
                break;
        }

    }


    @Override
    protected void setEditBeans() {
        zerenren = getIntent().getStringExtra(Config.GUID);
        if (!TextUtils.isEmpty(zerenren)) {
            isSave = false;
            isRead = false;
        }
        zerenrenName = getIntent().getStringExtra(Config.NAME);
        renwuDesc = getIntent().getStringExtra(Config.CONTENT);
        if (renwuDesc != null) {
            isSave = false;
            isRead = false;
        }

        datas.add(new SlimEditEnterBean("任务名称", "", 0, "请填写", null));
        datas.add(new SlimEditEnterBean("任务描述", renwuDesc == null ? "" : renwuDesc, 1, "请填写", null));
        datas.add(new SlimEditEnterBean("父项目", "", 2, true, "请选择（非必选）"));
        datas.add(new SlimEditEnterBean("责任人", zerenrenName == null ? "" : zerenrenName, 3, true, "请选择"));
        datas.add(new SlimEditEnterBean("协助人", "", 4, true, "请选择（非必选）"));
        datas.add(new SlimEditEnterBean("知会人", "", 5, true, "请选择（非必选）"));
        datas.add(new SlimEditTimeBean("开始时间", "", 6, "请选择").setType(Config.YEAR_MONTH_DAY_HOUR_MINITE_TYPE));
        datas.add(new SlimEditTimeBean("结束时间", "", 7, "请选择").setType(Config.YEAR_MONTH_DAY_HOUR_MINITE_TYPE));
        datas.add(new SlimEditEnterBean("验收标准", "", 8, "请填写", null));
        datas.add(new SlimEditEnterBean("验收人", "", 9, true, "请选择"));
        datas.add(new SlimEditTimeBean("验收时间", "", 10, "请选择").setType(Config.YEAR_MONTH_DAY_HOUR_MINITE_TYPE));
        datas.add(new SlimEditEnterBean("责任描述", "", 11, "请填写", null));

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Config.INTENT_CONTENT) {
            switch (requestCode) {
                case 0:
                case 1:
                case 8:
                case 11:
                    content = data.getStringExtra("content");
                    datas.get(requestCode).setContent(content);
                    slimAdapterEx.notifyItemChanged(requestCode);
                    break;
                case 3:
                    content = data.getStringExtra("content");
                    zerenren = data.getStringExtra("guid");
                    datas.get(requestCode).setContent(content);
                    slimAdapterEx.notifyItemChanged(requestCode);
                    break;
                case 4:
                    content = data.getStringExtra("content");
                    xiezuren = data.getStringExtra("guids");
                    datas.get(requestCode).setContent(content);
                    slimAdapterEx.notifyItemChanged(requestCode);
                    break;
                case 5:
                    content = data.getStringExtra("content");
                    zhihuiren = data.getStringExtra("guids");
                    datas.get(requestCode).setContent(content);
                    slimAdapterEx.notifyItemChanged(requestCode);
                    break;
                case 9:
                    content = data.getStringExtra("content");
                    yanshouren = data.getStringExtra("guid");
                    datas.get(requestCode).setContent(content);
                    slimAdapterEx.notifyItemChanged(requestCode);
                    break;
            }
        } else if (requestCode == Config.RENWU_LIST_SEARCH && resultCode == Config.SUCCESS) {
            renwuSearchBean = (RenwuSearchBean) data.getSerializableExtra("bean");
            datas.get(2).setContent(renwuSearchBean.getTaskName());
            slimAdapterEx.notifyItemChanged(2);
        }
    }
}
